package com.pl.controller;

import com.pl.common.JsonResult;
import com.pl.service.PictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author shent
 */
@RestController
@RequestMapping("picture")
public class PictureController {

    @Resource
    PictureService pictureService;

    /**
     * 前端发送图片到服务器,保存到服务器本地
     *
     * @param image 图片文件
     * @return 前端访问服务器图片的url
     */
    @RequestMapping("imgUpload")
    public JsonResult imgUpload(@RequestParam(value = "image") MultipartFile image) {
        JsonResult jsonResult;
        try {
            jsonResult = pictureService.savePicture(image);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(500, "图片上传失败");
        }
        return jsonResult;
    }

}
